package net.easoftware.code.beans;

import java.util.Arrays;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author eder.herrera
 */
@FacesConverter(forClass = Mes.class, value = "mesConverter")
public class MesConverter implements Converter {

    private List<String> arregloDeMeses = Arrays.asList("Ene", "Feb",
            "Mar", "Abr", "May", "Jun", "Jul",
            "Ago", "Sep", "Oct", "Nov",
            "Dic");

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        int numeroMes = Integer.parseInt(value);
        Mes mes = new Mes(numeroMes, arregloDeMeses.get(numeroMes));
        return mes;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value instanceof Mes ? ( (Mes) value ).getNumeroMes().toString() : "";
    }
}
